/**
 * Created by KaiBo on 2016/7/7.
 */
/**
 *
 * @param $showAddress
 * @param setting
 */

var addressLinkage = function ($showAddress,setting){
    $(function () {
        //初始化省地址
        var provinceUrl = "/Mall/AreaJson/AREA_ADDRESS_PROVINCE.json";
        var cityUrl = "/Mall/AreaJson/AREA_ADDRESS_CITY.json";
        var areaUrl = "/Mall/AreaJson/AREA_ADDRESS_AREA.json";

        function showAddress($superior, $subordinate, url) {
            $superior.change(function () {
                var val = $(this).val();
                val = val.replace(/'/g,"\"");
                val = JSON.parse(val); //可以将json字符串转换成json对象
                var id = val.id;
                $.getJSON(url, function (data) {
                    $subordinate.html("<option value></option>");
                    data = data[id];
                    for (var i in data) {//用javascript的for/in循环遍历对象的属性
                        var data2 = data[i];
                        val = JSON.stringify(data2); //可以将json对象转换成json对符串
                        val = val.replace(/"/g,"'");
                        var area = data2.area;
                        var html = "<option  value=" + val +">" + area + "</option>";
                        $subordinate.append(html);
                    }
                })
            });
        }

        var html = '<div class="col-sm-4">';
        html += '       <select name="PROVINCES" id="province" class="form-control col-xs-4"></select>';
        html += '   </div>';
        html += '   <div class="col-sm-4">';
        html += '        <select name="CITYS" id="city" class="form-control col-xs-4"></select>';
        html += '   </div>';
        html += '   <div class="col-sm-4">';
        html += '       <select name="AREAS" id="area"  class="form-control col-xs-4"></select>';
        html += '   </div>';

        $showAddress.html(html);

        var $province = $("#province");
        var $city = $("#city");
        var $area = $("#area");

        //初始化
        $.getJSON(provinceUrl, function (data) {
            data = data[1];
            $province.html("<option value></option>");
            for (var i in data) {//用javascript的for/in循环遍历对象的属性
                var data2 = data[i];
                var val = JSON.stringify(data2); //可以将json对象转换成json对符串
                val = val.replace(/"/g,"'");
                var area = data2.area;
                var html = "<option  value=" + val +">" + area + "</option>";
                $province.append(html);
            }
        });

        //绑定事件
        showAddress($province, $city, cityUrl);
        showAddress($city, $area, areaUrl);
    })
};